using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ClickerClimber.Models;


namespace ClickerClimber.Managers
{
    /// <summary>
    /// Componente de juego que implementa IUpdateable.
    /// </summary>
    public class RendererManager
    {
        private static RendererManager _instance;

        private SpriteBatch _spriteBatch;
        private ContentManager _content;
        private GraphicsDevice _device;
        private GraphicsDeviceManager _graphics;

        private List<IGameObjectDrawable> _renderObjectsList;


        private RendererManager()
        {
        }

        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static RendererManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new RendererManager();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Starts up the Singleton
        /// </summary>
        public void StartUp()
        {
            _spriteBatch = new SpriteBatch(_device);
            // Create a font
            _renderObjectsList = new List<IGameObjectDrawable>();
            this.Reset();
        }

        #region properties

        public GraphicsDevice Device
        {
            set
            {
                _device = value;
            }
            get
            {
                return _device;
            }
        }

        public ContentManager Content
        {
            set
            {
                _content = value;
            }
            get
            {
                return _content;
            }
        }

        public GraphicsDeviceManager Graphics
        {
            set
            {
                _graphics = value;
            }
        }

        #endregion properties

        #region public methods

        public void Draw()
        {
            _spriteBatch.Begin();

            foreach (IGameObjectDrawable drawable in _renderObjectsList)
            {
                drawable.Draw(_spriteBatch);
            }
            
            _spriteBatch.End();
        }

        public void AddGameObjectToRender(IGameObjectDrawable gameObject)
        {
            _renderObjectsList.Add(gameObject);
        }

        public void LoadScene(IGameObject gameObject)
        {
            gameObject.Initialize();

            IGameObjectDrawable drawable = gameObject as IGameObjectDrawable;
            if (drawable != null)
            {
                drawable.LoadTextures(_content);
                AddGameObjectToRender(drawable);
            }            
        }

        public void ChangeScene(IGameObject gameObject)
        {
            _renderObjectsList.Clear();
            LoadScene(gameObject);
        }

        //@deprecated
        public Texture2D LoadTexture(string textureName)
        {
            return _content.Load<Texture2D>(textureName); 
        }

        #endregion public methods

        #region support methods

        private void Reset()
        {
            _renderObjectsList.Clear();
        }

        #endregion support methods
    }
}
